package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Reference
    private UserInfoService userInfoService;
//   发送验证码
    @RequestMapping("/sendCode/{phone}")
    public Result sendCode(@PathVariable("phone") String phone, HttpServletRequest request){
//        设置验证码为8888
            String code="8888";
//        将验证码放到Session域中
            request.getSession().setAttribute("code",code);
//        将验证码响应到前端
        return Result.ok(code);
    }
//    注册
    @RequestMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpSession session){
//        获取手机号，密码，昵称和验证码
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String nickName = registerVo.getNickName();
        String code = registerVo.getCode();
//        验空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickName)|| StringUtils.isEmpty(code)){
//            返回参数错误的信息
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
//        判断验证码是否正确
        String sessionCode = (String)session.getAttribute("code");
        if(!code.equals(sessionCode)){
            return Result.build(null,ResultCodeEnum.CODE_ERROR);
        }
//        判断该手机号是否注册
        UserInfo userInfo= userInfoService.getUserInfoByPhone(phone);
        if(null!=userInfo){
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }
//        保存到数据库中
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setPhone(phone);
        userInfo1.setPassword(MD5.encrypt(password));
        userInfo1.setNickName(nickName);
        userInfo1.setStatus(1);
        userInfoService.insert(userInfo1);
        return Result.ok();
    }
//    登陆
    @RequestMapping("/login")
    public Result login(@RequestBody LoginVo loginVo,HttpSession session){
//        获得手机号和密码
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(phone)||StringUtils.isEmpty(password)){
            return Result.build(null,ResultCodeEnum.PARAM_ERROR);
        }
//        根据手机号查询用户
        UserInfo userInfo = userInfoService.getUserInfoByPhone(phone);
        if (userInfo==null){
            return Result.build(null,ResultCodeEnum.ACCOUNT_ERROR);
        }
//        验证密码是否正确
        if(!MD5.encrypt(password).equals(userInfo.getPassword())){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }
//        是否被锁定
        if(userInfo.getStatus()==0){
            return Result.build(null,ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
//        将用户信息放到session域中，为了在后台判断用户是否登录
        session.setAttribute("user",userInfo);
        Map map=new HashMap<>();
        map.put("nickName",userInfo.getNickName());
        map.put("phone",phone);
        return Result.ok(map);
    }
//    退出
    @RequestMapping("/logout")
    public Result logout(HttpSession session){
        session.removeAttribute("user");
        return Result.ok();
    }
}
